//
//  getAppInfo.m
//  gdemm
//
//  Created by 李园园 on 2017/8/14.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "getAppInfo.h"
#import "PackageInfo.h"
#import <objc/runtime.h>
#import "LSApplicationProxy.h"
#import "LSApplicationWorkspace.h"
#import <dlfcn.h>
#import "AppInfoModel.h"

@implementation getAppInfo

+(NSMutableArray *)getAllAppInfo{
     NSMutableArray *app = [NSMutableArray array];
    void *lib = dlopen("/System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices", RTLD_LAZY);
    
    if (lib)
    {
        Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
        id AAURLConfiguration1 = [LSApplicationWorkspace defaultWorkspace];
        
        if (AAURLConfiguration1)
        {
            id arrApp = [AAURLConfiguration1 allApplications];
            
            for (int i=0; i<[arrApp count]; i++) {
                
                LSApplicationProxy *LSApplication = [arrApp objectAtIndex:i];
                NSProgress *progress = (NSProgress *)[LSApplication installProgress];
                NSString *appBundleId = [LSApplication applicationIdentifier];
                NSString *appName = [LSApplication localizedName];
                
                NSString *appVersion = [LSApplication shortVersionString];
                NSString * type = [LSApplication applicationType];
                if ([type isEqualToString:@"User"]) {
                    PackageInfo *packageInfo = [[PackageInfo alloc]init];
                    packageInfo.softName = appName;
                    packageInfo.packageName = appBundleId;
                    packageInfo.version = appVersion;
                    if (progress) {
                         packageInfo.progress = progress.description;
                    }
                    [app addObject:packageInfo];
                }
            }
        }
        
        if (lib) dlclose(lib);
    }
    return app;
    
}

@end
